package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import com.blankj.utilcode.util.ColorUtils;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityGoodsChipBinding;
import com.swgk.core.view.entity.GoodsServicesEntity;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.dialog.RoundInputDialog;
import com.swgk.core.util.MToast;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author PC
 */
public class ChipActivity extends AppActivity implements View.OnClickListener {

    private ActivityGoodsChipBinding binding;
    List<GoodsServicesEntity> listService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(ChipActivity.this, R.layout.activity_goods_chip);
        binding.backView.setOnClickListener(this);
        binding.tvResult.setOnClickListener(this);

        listService = (List<GoodsServicesEntity>) getIntent().getSerializableExtra("listService");
        //循环遍历去除系统服务属性和自定义相同得情况，去除自定义属性
        for (int x = 0; x < listService.size(); x++) {
            for (int y = 0; y < listService.get(x).getValue().size(); y++) {
                for (int z = 0; z < listService.get(x).getValue().get(y).getValue().size(); z++) {
                    String attributeName = listService.get(x).getValue().get(y).getValue().get(z).getAttributeName();
                    for (int p = listService.get(x).getValue().get(y).getValue().size() - 1; p > z; p--) {
                        String attributeNameCopy = listService.get(x).getValue().get(y).getValue().get(p).getAttributeName();
                        if (attributeName.equals(attributeNameCopy)) {
                            //将与系统服务属性重名得去掉，并把选中状态赋值给系统服务属性
                            listService.get(x).getValue().get(y).getValue().get(p).setDeleted(1);
                            listService.get(x).getValue().get(y).getValue().get(p).setChecked(0);
                            listService.get(x).getValue().get(y).getValue().get(z).setChecked(listService.get(x).getValue().get(y).getValue().get(p).getChecked());
                        }
                    }
                }
            }
        }

        for (int i = 0; i < listService.size(); i++) {
            for (int j = 0; j < listService.get(i).getValue().size(); j++) {
                View view = this.getLayoutInflater().inflate(R.layout.goods_chip_item, null);
                //标题
                TextView tvTitle = view.findViewById(R.id.tv_title);
                tvTitle.setText(listService.get(i).getValue().get(j).getServerName());
                if ("1".equals(listService.get(i).getValue().get(j).getIsChange())) {
                    tvTitle.setTextColor(ColorUtils.getColor(R.color.color_EF4033));
                } else {
                    tvTitle.setTextColor(ColorUtils.getColor(R.color.FF333333));
                }
                //属性
                TagFlowLayout tagFlowLayout = view.findViewById(R.id.tag_flow_layout);
                //设置adapter
                int finalI = i;
                int finalJ = j;
                TagAdapter tagAdapter = new TagAdapter<GoodsServicesEntity.TypeValue.ItemValue>(listService.get(i).getValue().get(j).getValue()) {
                    @Override
                    public View getView(FlowLayout parent, final int position, GoodsServicesEntity.TypeValue.ItemValue s) {
                        LinearLayout iv = (LinearLayout) LayoutInflater.from(ChipActivity.this).inflate(R.layout.item_goods_chip, tagFlowLayout, false);
                        LinearLayout itemLayout = iv.findViewById(R.id.item_layout);
                        if ("1".equals(String.valueOf(s.getDeleted()))) {
                            itemLayout.setVisibility(View.GONE);
                        } else {
                            itemLayout.setVisibility(View.VISIBLE);
                        }

                        TextView tvType = iv.findViewById(R.id.type);
                        tvType.setText(s.getAttributeName());

                        if (s.getChecked() != null && "1".equals(String.valueOf(s.getChecked()))) {//0 未选中 1选中
                            itemLayout.setBackground(ContextCompat.getDrawable(ChipActivity.this, R.drawable.goods_chip_checked_bg));
                            tvType.setTextColor(ColorUtils.getColor(R.color.color_EF4033));
                        } else {
                            itemLayout.setBackground(ContextCompat.getDrawable(ChipActivity.this, R.drawable.goods_chip_unchecked_bg));
                            tvType.setTextColor(ColorUtils.getColor(R.color.color_666666));
                        }
                        ImageView deleteImg = iv.findViewById(R.id.delete);
                        if (s.getCustomed() != null && "1".equals(String.valueOf(s.getCustomed()))) {//0 不是自定义，1 自定义
                            deleteImg.setVisibility(View.VISIBLE);
                        } else {
                            deleteImg.setVisibility(View.GONE);
                        }
                        deleteImg.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                new RoundDialog.Builder(ChipActivity.this)
                                        .title("温馨提示")
                                        .content("删除后不可恢复，您还要继续么？")
                                        .cancel("取消")
                                        .confirm("确定")
                                        .singleBtn(false)
                                        .contentGravity(Gravity.CENTER)
                                        .cancelable(false)
                                        .confirmCallback(view -> {
                                            listService.get(finalI).getValue().get(finalJ).getValue().get(position).setDeleted(1);
                                            listService.get(finalI).getValue().get(finalJ).getValue().get(position).setChecked(0);
                                            notifyDataChanged();
                                        })
                                        .show();
                            }
                        });
                        return iv;
                    }
                };
                tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                    @Override
                    public boolean onTagClick(View view, int position, FlowLayout parent) {
                        if ("0".equals(String.valueOf(listService.get(finalI).getValue().get(finalJ).getValue().get(position).getChecked()))) {
                            listService.get(finalI).getValue().get(finalJ).getValue().get(position).setChecked(1);
                        } else {
                            listService.get(finalI).getValue().get(finalJ).getValue().get(position).setChecked(0);
                        }
                        tagAdapter.notifyDataChanged();
                        return true;
                    }
                });
                tagFlowLayout.setAdapter(tagAdapter);
                //自定义按钮
                TextView tvAdd = view.findViewById(R.id.tv_add);
                //0 允许自定义 1不允许自定义
                if ((listService.get(finalI).getValue().get(finalJ).getServerCustomed()) != null && "0".equals(String.valueOf(listService.get(finalI).getValue().get(finalJ).getServerCustomed()))) {
//                    tvAdd.setText("这个可以自定义");
                    tvAdd.setVisibility(View.VISIBLE);
                } else {
//                    tvAdd.setText("这个不可以自定义");
                    tvAdd.setVisibility(View.GONE);
                }
                tvAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        List<GoodsServicesEntity.TypeValue.ItemValue> systemTempEntity = new ArrayList<>();
                        for (int a = 0; a < listService.get(finalI).getValue().get(finalJ).getValue().size(); a++) {
                            if ("1".equals(String.valueOf(listService.get(finalI).getValue().get(finalJ).getValue().get(a).getCustomed()))&&"0".equals(String.valueOf(listService.get(finalI).getValue().get(finalJ).getValue().get(a).getDeleted()))) {
                                systemTempEntity.add(listService.get(finalI).getValue().get(finalJ).getValue().get(a));
                            }
                        }
                        if (systemTempEntity.size() >= 10) {
                            MToast.showToast(ChipActivity.this, "最多添加十条自定义服务！");
                            return;
                        }
                        RoundInputDialog dialog = new RoundInputDialog(ChipActivity.this);
                        dialog.setConfirmCallback(new RoundInputDialog.ConfirmCallback() {
                            @Override
                            public void onClick(View v, String intPutString) {
                                if (TextUtils.isEmpty(intPutString)) {
                                    dialog.setErrorMessage("请输入自定义服务！");
                                    return;
                                }
                                for (int j = 0; j < listService.get(finalI).getValue().get(finalJ).getValue().size(); j++) {
                                    if (intPutString.equals(listService.get(finalI).getValue().get(finalJ).getValue().get(j).getAttributeName()) && "0".equals(String.valueOf(listService.get(finalI).getValue().get(finalJ).getValue().get(j).getDeleted()))) {
                                        dialog.setErrorMessage("该服务名称已存在，请重新输入！");
                                        return;
                                    }
                                }
                                GoodsServicesEntity.TypeValue.ItemValue itemValue = new GoodsServicesEntity.TypeValue.ItemValue();
                                itemValue.setAttributeName(intPutString);
                                itemValue.setChecked(0);
                                itemValue.setCustomed(1);
                                itemValue.setDeleted(0);
                                itemValue.setServerName(listService.get(finalI).getValue().get(finalJ).getServerName());
                                listService.get(finalI).getValue().get(finalJ).getValue().add(itemValue);
                                tagAdapter.notifyDataChanged();
                                dialog.dismiss();
                            }
                        });
                        dialog.show();
                    }
                });
                binding.contextLayout.addView(view);
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.tv_result:
                Intent intent = getIntent();
                intent.putExtra("listService", (Serializable) listService);
                setResult(RESULT_OK, intent);
                finish();
                break;
            default:
                break;
        }
    }
}